//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_side","_param","_kamikaze","_units","_distance","_ammo","_trust","_trigger","_destroy","_pos","_explo"];
if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"mission_functions.sqf","XfMissionKamikaze",_this];};
_side = _this select 0;
if (typeName _side != typename east) exitWith {};
_param = _this select 1;
if (isNil "_param") exitWith {};
if (typeName _param == "OBJECT") then {
	_kamikaze = _param;
} else {
	if (typeName _param == "GROUP") then {
		_units = units _param;
		_kamikaze = _units select (floor (random (count _units)));
	};
};
if (typeName _kamikaze != "OBJECT") exitWith {};
_distance = if (count _this > 2) then {_this select 2;} else {15;};
if (typeName _distance != "SCALAR") exitWith {};
_ammo = if (count _this > 3) then {_this select 3;} else {"R_57mm_HE";};
if (typeName _ammo != "STRING") exitWith {};
_trust = if (count _this > 4) then {_this select 4;} else {1;};
if (typeName _trust != "SCALAR") exitWith {};
_kamikaze allowFleeing 0;
_kamikaze setskill ["courage",1];
_kamikaze setskill ["spotDistance",0];
_kamikaze setskill ["spotTime",0];
_kamikaze disableAI "AUTOTARGET";
_kamikaze disableAI "TARGET";
_trigger = false;
_destroy = false;
_target = objNull;
if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: Created: %4",diag_tickTime,"mission_functions.sqf","XfMissionKamikaze",[typeof _kamikaze, getPos _kamikaze, _distance, _ammo, _trust]];};
while {!_trigger && !(isNull _kamikaze)} do {
	_pos = position _kamikaze;
	_units = nearestObjects [_pos,["Man","Car","Tank","Motorcycle"],_distance];
	if (count _units > 0) then {
		{	if ((side _x == _side) && alive _x) exitWith {_trigger = true; _target = _x;};
			sleep 0.01;
		} forEach _units;
	};
	if ((damage (vehicle _kamikaze) > 0.7) || !(alive _kamikaze)) then {_trigger = true;_destroy = true;};
	if (_trigger) then {
		if (!isnull _target && (alive _kamikaze)) then {
			_i_runs = 30;
			_kamikaze doMove (getPos _target);
			sleep 0.5123;
			_kamikaze doMove (getPos _target);
			_last_target_pos = getPos _target;
			_last_kam_pos = getPos _kamikaze;
			while { (_target distance _kamikaze) > 15 && _i_runs > 0 && (alive _kamikaze)} do {
				if ((_last_target_pos distance _target) > 3 || (_last_kam_pos distance _kamikaze) < 1) then {
					_last_target_pos = getPos _target;
					_kamikaze doMove (getPos _target);
				};
				_last_kam_pos = getPos _kamikaze;
				sleep 0.5123;
			};
		};
		(random 1);
		if (!(alive _kamikaze)) then { _destroy = true; };
		if (((random 1 <= _trust) && (_trust > 0)) || _destroy) then {
			_distance = 0;
			if (alive _kamikaze) then {
				_distance = (speed _kamikaze) / 3;
				_pos = [_kamikaze,_distance,direction _kamikaze] call BIS_fnc_relPos;
			} else {
				_pos = getPos _kamikaze;
			};
			if (vehicle _kamikaze != _kamikaze) then {(vehicle _kamikaze) setDamage 1; };
			_explo = createVehicle [_ammo,_pos,[],0,"CAN_COLLIDE"];
			_explo setDamage 1;
			_kamikaze setDamage 1;
			if (!isNil "ACV_FNCT_Broadcast_BIS_Effects_Burn") then {[_pos,5,40] spawn ACV_FNCT_Broadcast_BIS_Effects_Burn;};
		} else {
			_pos = position _kamikaze;
			_fumi = createVehicle ["SmokeShell",_pos,[],0,"CAN_COLLIDE"];
			_fumi setDamage 1;
			_fumi attachTo [_kamikaze,[0,0,0.5]];
			sleep 3;
			deleteVehicle _fumi;
		};
	};
	sleep 1;
};

TRACE("End");
TMFPROFILERSTOP;